package com.teacold.customdemo.chapter_one;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 画布的一系列方法
 */
public class CanvasDemo extends View {
    public CanvasDemo(Context context) {
        super(context);
    }

    public CanvasDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CanvasDemo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        camvasSave(canvas);


    }

    /**
     * canvas 保存于恢复
     */
    private void camvasSave(Canvas canvas) {
        canvas.drawColor(Color.RED);
        //保存画布的大小
        canvas.save();
        canvas.clipRect(new RectF(100, 100, 800, 800));
        canvas.drawColor(Color.GREEN);
        canvas.save();

        canvas.clipRect(new Rect(200, 200, 700, 700));
        canvas.drawColor(Color.BLUE);
        //保存画布大小为Rect(200,200,700,700)
        canvas.save();

        canvas.clipRect(new Rect(300, 300, 600, 600));
        canvas.drawColor(Color.BLACK);
        canvas.save();

        canvas.clipRect(new Rect(400,400,500,500));
        canvas.drawColor(Color.BLUE);
    }

    /**
     * canvas 剪切
     */
    private void camvasClip(Canvas canvas) {
//        Paint paint_green=generatePatint(Color.GREEN,Paint.Style.STROKE,3);
//        Paint paint_red=generatePatint(Color.RED,Paint.Style.STROKE,3);
//
        Rect rect = new Rect(10, 10, 200, 200);
        canvas.drawColor(Color.RED);
        canvas.clipRect(rect);
        canvas.drawColor(Color.GREEN);
    }

    /**
     * canvas 扭曲
     */
    private void camvasSkew(Canvas canvas) {
        Paint paint_green = generatePatint(Color.GREEN, Paint.Style.STROKE, 3);
        Paint paint_red = generatePatint(Color.RED, Paint.Style.STROKE, 3);

        Rect rect = new Rect(10, 10, 200, 100);
        canvas.drawRect(rect, paint_green);
        canvas.skew(1.732f, 0);//X轴倾斜60°，Y轴不变
        canvas.drawRect(rect, paint_red);
    }

    /**
     * canvas 放大缩小
     */
    private void camvasScale(Canvas canvas) {
        Paint paint_green = generatePatint(Color.GREEN, Paint.Style.STROKE, 3);
        Paint paint_red = generatePatint(Color.RED, Paint.Style.STROKE, 3);

        Rect rect = new Rect(200, 200, 300, 220);
        canvas.drawRect(rect, paint_green);
        canvas.scale(1, 3);
        canvas.drawRect(rect, paint_red);
    }

    /**
     * canvas 旋转
     */
    private void camvasRotate(Canvas canvas) {
        Paint paint_green = generatePatint(Color.GREEN, Paint.Style.STROKE, 3);
        Paint paint_red = generatePatint(Color.RED, Paint.Style.STROKE, 3);

        Rect rect = new Rect(200, 200, 300, 220);
        canvas.drawRect(rect, paint_green);
        canvas.rotate(30);
        canvas.drawRect(rect, paint_red);
    }

    /**
     * canvas 平移数据
     */
    private void camvasTranslate(Canvas canvas) {
        Paint paint_green = generatePatint(Color.GREEN, Paint.Style.STROKE, 3);
        Paint paint_red = generatePatint(Color.RED, Paint.Style.STROKE, 3);
        //构造一个矩形
        RectF rectF = new RectF(0, 0, 400, 220);
        //在平移画布前用绿色画笔画下边框
        canvas.drawRect(rectF, paint_green);
        //在平移画布后，在用红色画笔重新画下边框
        canvas.translate(100, 100);
        canvas.drawRect(rectF, paint_red);
    }


    /**
     * 设置画笔的基础设施
     */
    private Paint generatePatint(int color, Paint.Style style, int width) {
        Paint paint = new Paint();
        paint.setStyle(style);
        paint.setColor(color);
        paint.setStrokeWidth(width);
        return paint;
    }

}
